﻿using UnityEngine;
using System.Collections;

namespace Game.Player
{
    /// <summary>
    /// 用于作为玩家物体的代理。
    /// <para>对于自认为Player的类，需要其自行设置此类以供其他需要知道Player的类使用</para>
    /// </summary>
	public static class PlayerDelegate 
	{
        /// <summary>
        /// 玩家物体被改变时的回调
        /// </summary>
        static event CallbackFuncWithGameObj g_cbOnPlayerChanged;

		//---------------------------------------------------------------------------
		//				Public area
		//---------------------------------------------------------------------------
		#region Public area


        /// <summary>
        /// 玩家物体实例
        /// </summary>
        public static Transform playerTransform
        {
            get; private set;
        }

        /// <summary>
        /// 设置为PlayerTransform所在
        /// </summary>
        public static void SetPlayerTransform(Transform playerTrans)
        {
            playerTransform = playerTrans;
            if (g_cbOnPlayerChanged != null)
            {
                g_cbOnPlayerChanged(playerTransform.gameObject);
            }
        }
        /// <summary>
        /// 加入监听：当玩家物体被改变时触发
        /// </summary>
        /// <param name="onPlayerChanged">触发事件</param>
        public static void ListenOnPlayerChanged(CallbackFuncWithGameObj onPlayerChanged)
        {
            g_cbOnPlayerChanged += onPlayerChanged;
        }

        public static float GetSqrDistanceToPlayer(Transform srcTrans,bool isSqrDistance=true)
        {
            if(playerTransform==null)
            {
                Debug.LogWarning("No player trans found");
                return float.MaxValue;
            }


            float sqrDistance = playerTransform.SqrDistanceTo(srcTrans);
            if(!isSqrDistance)
            {
                sqrDistance = Mathf.Sqrt(sqrDistance);
            }

            return sqrDistance;
        }

		#endregion

		//---------------------------------------------------------------------------
		//				Private area
		//---------------------------------------------------------------------------
		#region Private area
		#endregion
	}
}